package com.osell.selection.ui.main;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.fragment.MineFragment;
import com.osell.selection.ui.base.BaseActivity;
import com.osell.selection.util.FragmentSwitchHelper;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rg_bottom_navigation)
    RadioGroup rg_bottom_navigation;
    @BindView(R.id.rb_home)
    RadioButton rb_home;
    @BindView(R.id.rb_message)
    RadioButton rb_message;
    @BindView(R.id.rb_silk)
    RadioButton rb_silk;
    @BindView(R.id.rb_store)
    RadioButton rb_store;
    @BindView(R.id.rb_me)
    RadioButton rb_me;
    @BindView(R.id.v_divider)
    ImageView v_divider;

    private FragmentSwitchHelper mFragmentSwitchHelper;

    private int mCheckedId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_2);
        ButterKnife.bind(this);

        setupWhiteTheme();
        rg_bottom_navigation.setOnCheckedChangeListener(this);
        mFragmentSwitchHelper = FragmentSwitchHelper.create(
                R.id.fl_main_fragment_container,
                getSupportFragmentManager(),
                true,
                new HomeFragment(),
                new MessageFragment(),
                new SilkFragment(),
                new StoreFragment(),
                new MineFragment());

        rg_bottom_navigation.check(R.id.rb_home);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (mCheckedId == checkedId) {
            return;
        }
        switch (checkedId) {
            case R.id.rb_home: {
                setupWhiteTheme();
                mFragmentSwitchHelper.changeFragment(HomeFragment.class);
            }
            break;
            case R.id.rb_message: {
                setupRedTheme();
                mFragmentSwitchHelper.changeFragment(MessageFragment.class);
            }
            break;
            case R.id.rb_silk: {
                setupRedTheme();
                mFragmentSwitchHelper.changeFragment(SilkFragment.class);
            }
            break;
            case R.id.rb_store: {
                setupRedTheme();
                mFragmentSwitchHelper.changeFragment(StoreFragment.class);
            }
            break;
            case R.id.rb_me: {
                setupRedTheme();
                mFragmentSwitchHelper.changeFragment(MineFragment.class);
            }
            break;
            default:
                break;
        }
        mCheckedId = checkedId;
    }

    private void setupRedTheme() {
        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_home, R.color.selector_tab_text_color), null, null);
        rb_message.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_message, R.color.selector_tab_text_color), null, null);
        rb_silk.setCompoundDrawablesWithIntrinsicBounds(null, null, null, buildSelectorDrawable(R.drawable.ic_silk_tab, R.color.selector_tab_text_color));
        rb_store.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_store, R.color.selector_tab_text_color), null, null);
        rb_me.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_me, R.color.selector_tab_text_color), null, null);
        rb_home.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color));
        rb_message.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color));
        rb_silk.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color));
        rb_store.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color));
        rb_me.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color));

        rb_silk.setBackgroundResource(R.drawable.shape_circle_white);
        v_divider.setImageDrawable(buildTintDrawable(R.drawable.navigation_bottom_divider, R.color.colorDivider));
    }

    private void setupWhiteTheme() {
        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_home, R.color.selector_tab_text_color_white), null, null);
        rb_message.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_message, R.color.selector_tab_text_color_white), null, null);
        rb_silk.setCompoundDrawablesWithIntrinsicBounds(null, null, null, buildSelectorDrawable(R.drawable.ic_silk_tab, R.color.selector_tab_text_color_white));
        rb_store.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_store, R.color.selector_tab_text_color_white), null, null);
        rb_me.setCompoundDrawablesWithIntrinsicBounds(null, buildSelectorDrawable(R.drawable.ic_me, R.color.selector_tab_text_color_white), null, null);
        rb_home.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color_white));
        rb_message.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color_white));
        rb_silk.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color_white));
        rb_store.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color_white));
        rb_me.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_tab_text_color_white));

        rb_silk.setBackgroundResource(R.drawable.shape_circle_white_tran);
        v_divider.setImageDrawable(buildTintDrawable(R.drawable.navigation_bottom_divider, R.color.seventy_percent_transparency_white));
    }

    private Drawable buildSelectorDrawable(@DrawableRes int drawableRes, @ColorRes int colorRes) {
        Drawable drawable = ContextCompat.getDrawable(this, drawableRes);
        assert drawable != null;
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, ContextCompat.getColorStateList(this, colorRes));
        return drawable;
    }

    private Drawable buildTintDrawable(@DrawableRes int drawableRes, @ColorRes int colorRes) {
        Drawable drawable = ContextCompat.getDrawable(this, drawableRes);
        assert drawable != null;
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, ContextCompat.getColor(this, colorRes));
        return drawable;
    }
}
